<?php
namespace frontend\controllers;

use common\models\IndexImg;
use Yii;
use yii\web\Controller;
use common\models\YmhSeo;
use common\models\Banner;
use common\models\Email;
use common\models\Product;
use common\models\Videos;

class IndexController extends Controller {


    public function actionIndex() {
        $seo=YmhSeo::findOne(1);
        //var_dump($seo);die();
        $view = Yii::$app->view;
        $view->params['layout_data']['menu'] = 'index';
        $view->params['layout_data']['seo'] = $seo;
        $banner = Banner::find()->orderBy('sort ASC')->all();
        $pro_list = Product::getProCat();
        $view->params['layout_data']['pro_list'] = $pro_list;
        $model_v = IndexImg::getVideoList();
        foreach ($model_v as $key=>$value) {
            $pages[$key] =  $value['page'];
        }
        $pages = array_unique($pages);
        sort($pages);
//        var_dump($pages);die();
        return $this->render("index",['pro_list'=>$pro_list,'model_v'=>$model_v,'pages'=>$pages]);
    } 
    
    
    public function actionGetMail() {
        $request = Yii::$app->request;
        if($request->isPost){
            $model = new Email;
            $mail = $request->post('email');
            $isMail = $this->check_email($mail);
            $result = $model->find()->where(['email'=>$mail])->one();
//            echo $isMail;die();
            if(!$result && $isMail){
                $model->email = $mail;
                $model->ip = Yii::$app->request->userIP;
                $model->add_time = time();
                if($model->save()){
                    $data['msg'] = '提交成功';
                } else {
                    $data['msg'] = '提交失败，请稍候再试！';
                }
            } else {
                $data['msg'] = '该邮箱已存在或邮件格式不会！';
            }
            echo json_encode($data);
            return;
            
        }
    }
    
    
    public function actionMIndex(){
        $this->layout = 'main_local';
        $view = Yii::$app->view;
        $view->params['layout_data']['menu'] = 'index';
        $seo=YmhSeo::findOne(1);
        $view->params['layout_data']['seo'] = $seo;
        $banner = Banner::find()->all();
        $pro_list = Product::getProCat();
        $view->params['layout_data']['pro_list'] = $pro_list;
        $model_v = IndexImg::getVideoList();
        foreach ($model_v as $key=>$value) {
            $pages[$key] =  $value['page'];
        }
        $pages = array_unique($pages);
        sort($pages);
//        var_dump($pages);die();
        return $this->render("m-index",['banner'=>$banner,'pro_list'=>$pro_list,'model_v'=>$model_v,'pages'=>$pages]);
    }

    
    protected function check_email($mail){
        $checkmail="/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/";
        if (preg_match($checkmail,$mail)){
            return true;
        } else {
            return false;
        }
    }

}

?>